package com.dalimao.floateutil.widget;

import com.dalimao.floateutil.util.MyUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class LoadingWidget extends DependentLayout {
    //定义三个小球
    CircularView leftView,rightView,middleView;
    //动画位移的距离
    int DISPLACEMENT=25;
    //动画执行的时间
    int DURATION_TIME=1000;

    public LoadingWidget(Context context) {
        this(context,null);
    }

    public LoadingWidget(Context context, AttrSet attrs) {
        this(context, attrs,null);
    }

    public LoadingWidget(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);

        DISPLACEMENT=px2dip(DISPLACEMENT);

        leftView=getCircularView(context);
        rightView=getCircularView(context);
        middleView=getCircularView(context);


        leftView.setColor(Color.BLUE);
        rightView.setColor(Color.RED);
        middleView.setColor(Color.GREEN);

        addComponent(leftView);
        addComponent(rightView);
        addComponent(middleView);


        // 创建背景元素
        ShapeElement shapeElement = new ShapeElement();
        // 设置颜色
        shapeElement.setRgbColor(MyUtils.getRgbColor("#ffffff"));
        setBackground(shapeElement);
    }

    private CircularView getCircularView(Context context){
        CircularView circularView=new CircularView(context);
        //设置小球的大小
        DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(px2dip(10),px2dip(10));
        //设置小球水平居中
        layoutConfig.addRule(LayoutConfig.CENTER_IN_PARENT);
        circularView.setLayoutConfig(layoutConfig);
        return circularView;
    }

    private int px2dip(int px){
        return AttrHelper.fp2px(px,mContext);
    }

    //展开动画
    public void openAnimator(){
        //左边
        AnimatorProperty animator1 = new AnimatorProperty();
        animator1.setTarget(leftView);
        animator1.moveFromX(getEstimatedWidth()/2)
                .moveToX(getEstimatedWidth()/2 - DISPLACEMENT-leftView.getEstimatedWidth());


        AnimatorProperty animator2 = new AnimatorProperty();
        animator2.setTarget(rightView);
        animator2.moveFromX(getEstimatedWidth()/2).moveToX(DISPLACEMENT+getEstimatedWidth()/2);

        AnimatorGroup group = new AnimatorGroup();
        group.setDuration(DURATION_TIME);
        group.setCurveType(Animator.CurveType.DECELERATE);
        group.runParallel(animator1,animator2);
        group.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {

                Color leftColor=leftView.getColor();
                Color middleColor=middleView.getColor();
                Color rightColor=rightView.getColor();
                //切换小球的原色
                middleView.setColor(rightColor);
                leftView.setColor(middleColor);
                rightView.setColor(leftColor);

                //展开动画完成后 调用收拢动画
                closeAnimator();
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });


        group.start();
    }

    //关闭动画
    private void closeAnimator(){
        //左边
        AnimatorProperty animator1 = new AnimatorProperty();
        animator1.setTarget(leftView);
        animator1.moveFromX(getEstimatedWidth()/2-DISPLACEMENT).moveToX(getEstimatedWidth()/2);

        //右边
        AnimatorProperty animator2 = new AnimatorProperty();
        animator2.setTarget(rightView);
        animator2.moveFromX(getEstimatedWidth()/2+DISPLACEMENT)
                .moveToX(getEstimatedWidth()/2-rightView.getEstimatedWidth());


        AnimatorGroup group = new AnimatorGroup();
        group.setDuration(DURATION_TIME);
        group.setCurveType(Animator.CurveType.DECELERATE);

        //无限循环
        group.runParallel(animator1,animator2);
        group.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                openAnimator();
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        group.start();
    }
}
